<?php
namespace AuctionCore\CarDealer;

use AuctionCore\System\ListBase;
use AuctionCore\CarResource\CarModel;
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//


/**
* "待收车商车款拍单列表"列表
* @author       爱民
*/
class WaitCollectMoneyList extends ListBase
{

    private $base_sql = null;
    private $count_sql = null;

    public function __construct() {

        // SELECT ao.order_id,ao.order_no,ao.status,ao.dealer_pay_status,ao.bid_better_dealer_id,d.dealer_id,d.fullname,d.shortname,d.contact_person,d.contact_mobile,pay.createtime pay_time FROM au_order ao USE INDEX (idx_order_status) LEFT JOIN au_dealer_auctioned aa  ON  ao.order_id = aa.order_id
        //         LEFT JOIN au_car_dealer d ON aa.dealer_id = d.dealer_id
        //         LEFT JOIN au_deal_pay_log pay ON ao.order_id = pay.order_id   WHERE ((ao.`status` = 8/*待付给车主首款*/ AND ao.dealer_pay_status=1/*车商未付款*/ )
        //         OR (ao.`status` != 6/*没有流拍*/ AND ao.`status` != 12/*没有失败*/ AND ao.bid_better_dealer_id >0/*有第二名*/  ))  ORDER BY ao.order_id ASC  LIMIT 0,10;

        $prefix = C('DB_PREFIX');
        $query_sql = "SELECT ao.order_id,ao.order_no,ao.status,ao.dealer_pay_status,ao.success_dealer_id,
                car.car_id,car.mbrand_id,car.brand_id,car.series_id,car.model_id,
                d.dealer_id,d.fullname,d.shortname,d.contact_person,d.`contact_mobile`,pay.createtime pay_time";
        $sql = " FROM {$prefix}order ao USE INDEX (idx_order_status) LEFT JOIN {$prefix}cars car ON ao.car_id = car.car_id ";
        $sql2= "LEFT JOIN {$prefix}car_dealer d ON d.dealer_id = ao.success_dealer_id
                LEFT JOIN {$prefix}proceeds_log pay ON(ao.order_id = pay.order_id) ";
        //$this->condition = "";
        $this->base_sql = $query_sql.$sql.$sql2. " #condition# #order_by# #limit# ";
        $this->count_sql = "select count(*) count ".$sql.$sql2 ;
    }


    /**
    * 获取列表结果
    * @param    int $ev_page_num    每页条数
    * @param    int $page    页码
    * @return   Object
    */
    public function get($ev_page_num, $page){
        if(!$this->order_by){
            $this->order_by = 'ORDER BY pay.createtime DESC';
        }
        $Model = new \Think\Model();
        //拍单列表信息
        $sql = $this->base_sql;
        $sql = str_replace('#order_by#', $this->order_by, $sql);
        $sql = str_replace('#condition#', $this->condition, $sql);
        $skip = ($page<2?0:$ev_page_num*($page-1)) ;
        $limit = " limit {$skip},{$ev_page_num}" ;
        $sql = str_replace('#limit#', $limit, $sql);
        $list = $Model->query($sql);        
		$cm = new CarModel();
        foreach ($list as $key => $value) {
            $brand = $cm->get_model_by_id($value['brand_id'],'brand');
            $series = $cm->get_model_by_id($value['series_id'],'series');
            $model = $cm->get_model_by_id($value['model_id']);
            $list[$key]['model'] = $brand['name'].' '.$series['name'].' '.$model['name'];
        }
        return $list;
    }

    public function count(){
        $Model = new \Think\Model();
        $sql = $this->count_sql;
        $sql .= " {$this->condition}";
        $sql .= ";";
        //dump($sql);
        $r = $Model->query($sql);
        return $r[0]['count'];
    }

}

?>